// pages/myStore/myStore.js
const call = require("../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myStoreList: [],
    pageNum: 1,
    isLoading: false,
    bottomTip: '',
  },

  myStoretoDetail (e) {
    console.log(e)
    const jobId = e.currentTarget.dataset.id
    if(e.currentTarget.dataset.open){
      wx.navigateTo({
        url: '/pages/jobDetail/jobDetail?jobId='+jobId,
      })
    }
  },
  
  // 长按可删除
  toDelete (e) {
    console.log(e)
    const that = this
    const jobId = e.currentTarget.dataset.id
    const myStoreList = this.data.myStoreList
    wx.showModal({
      content: '取消收藏吗',
      confirmColor: '#99ccff',
      success(res) {
        if (res.confirm) {
          const openId = wx.getStorageSync('token') || '';
          if (openId) {
            call.requestGet('jus/goStore', {
              openId: openId,
              id: jobId
            }, function (e) {
              const myNewStore = myStoreList.filter(item => item.id != jobId)
              that.setData({
                myStoreList: myNewStore
              })
            }, function () {

            });
          } else {
            // 弹窗，让用户授权
            wx.navigateTo({
              url: '../authPage/authPage'
            })
          }
         } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  getMyStoreList: function() {
    const openId = wx.getStorageSync('token');
    const that = this
    this.setData({
      pageNum: 1,
      isLoading: false,
      bottomTip: '',
    })
    if (openId) {
      call.requestGet('jus/findAllStore', {
        openId: openId,
        pageNum: 1
      }, function (e) {
        console.log(e)
        that.setData({
          myStoreList: e
        })
      }, function () {

      })
    } else {
      wx.navigateTo({
        url: '../authPage/authPage'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMyStoreList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("触底了")
    const that = this
    const openId = wx.getStorageSync('token');
    that.setData({
      isLoading: true,
      bottomTip: ''
    })
    const pageNum = ++that.data.pageNum
    call.requestGet('jus/findAllStore',
      {
        openId: openId,
        pageNum: pageNum
      },
      function (res) {
        console.log(res)
        if (res.length) {
          that.setData({
            isLoading: false,
            myStoreList: that.data.myStoreList.concat(res)
          })
        } else {
          that.setData({
            isLoading: false,
            bottomTip: '哎哟，都被你看光了～',
            pageNum: pageNum - 1
          })
        }
      },
      function (res) {
        console.log(res)
        that.setData({
          isLoading: false,
          bottomTip: '啊喔，加载失败～'
        })
      }
    )
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})